<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-CGIHTTPServer.html" />
<link rel="prev" href="module-BaseHTTPServer.html" />
<link rel="parent" href="internet.html" />
<link rel="next" href="module-CGIHTTPServer.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>18.20 SimpleHTTPServer -- Simple HTTP request handler</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="18.19 basehttpserver  "
  href="module-BaseHTTPServer.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="18. internet Protocols and"
  href="internet.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="18.21 cgihttpserver  "
  href="module-CGIHTTPServer.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-BaseHTTPServer.html">18.19 BaseHTTPServer  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="internet.html">18. Internet Protocols and</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-CGIHTTPServer.html">18.21 CGIHTTPServer  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION00202000000000000000000">
18.20 <tt class="module">SimpleHTTPServer</tt> --
         Simple HTTP request handler</a>
</h1>

<p>
<a name="module-SimpleHTTPServer"></a>

<p>
The <tt class="module">SimpleHTTPServer</tt> module defines a request-handler class,
interface-compatible with <tt class="class">BaseHTTPServer.BaseHTTPRequestHandler</tt>,
that serves files only from a base directory.

<p>
The <tt class="module">SimpleHTTPServer</tt> module defines the following class:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-4349' xml:id='l2h-4349' class="class">SimpleHTTPRequestHandler</tt></b>(</nobr></td>
  <td><var>request, client_address, server</var>)</td></tr></table></dt>
<dd>
This class is used to serve files from the current directory and below,
directly mapping the directory structure to HTTP requests.

<p>
A lot of the work, such as parsing the request, is done by the base
class <tt class="class">BaseHTTPServer.BaseHTTPRequestHandler</tt>.  This class
implements the <tt class="function">do_GET()</tt> and <tt class="function">do_HEAD()</tt> functions.
</dl>

<p>
The <tt class="class">SimpleHTTPRequestHandler</tt> defines the following member
variables:

<p>
<dl><dt><b><tt id='l2h-4350' xml:id='l2h-4350' class="member">server_version</tt></b></dt>
<dd>
This will be <code>"SimpleHTTP/" + __version__</code>, where <code>__version__</code>
is defined in the module.
</dl>

<p>
<dl><dt><b><tt id='l2h-4351' xml:id='l2h-4351' class="member">extensions_map</tt></b></dt>
<dd>
A dictionary mapping suffixes into MIME types. The default is signified
by an empty string, and is considered to be <code>application/octet-stream</code>.
The mapping is used case-insensitively, and so should contain only
lower-cased keys.
</dl>

<p>
The <tt class="class">SimpleHTTPRequestHandler</tt> defines the following methods:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4352' xml:id='l2h-4352' class="method">do_HEAD</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
This method serves the <code>'HEAD'</code> request type: it sends the
headers it would send for the equivalent <code>GET</code> request. See the
<tt class="method">do_GET()</tt> method for a more complete explanation of the possible
headers.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4353' xml:id='l2h-4353' class="method">do_GET</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
The request is mapped to a local file by interpreting the request as
a path relative to the current working directory.

<p>
If the request was mapped to a directory, the directory is checked for
a file named <code>index.html</code> or <code>index.htm</code> (in that order).
If found, the file's contents are returned; otherwise a directory
listing is generated by calling the <tt class="method">list_directory()</tt> method.
This method uses <tt class="function">os.listdir()</tt> to scan the directory, and
returns a <code>404</code> error response if the <tt class="function">listdir()</tt> fails.

<p>
If the request was mapped to a file, it is opened and the contents are
returned.  Any <tt class="exception">IOError</tt> exception in opening the requested
file is mapped to a <code>404</code>, <code>'File not found'</code>
error. Otherwise, the content type is guessed by calling the
<tt class="method">guess_type()</tt> method, which in turn uses the
<var>extensions_map</var> variable.

<p>
A <code>'Content-type:'</code> header with the guessed content type is
output, followed by a <code>'Content-Length:'</code> header with the file's
size and a <code>'Last-Modified:'</code> header with the file's modification
time.

<p>
Then follows a blank line signifying the end of the headers,
and then the contents of the file are output. If the file's MIME type
starts with <code>text/</code> the file is opened in text mode; otherwise
binary mode is used.

<p>
For example usage, see the implementation of the <tt class="function">test()</tt>
function.

<span class="versionnote">New in version 2.5:
The <code>'Last-Modified'</code> header.</span>

</dl>

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-BaseHTTPServer.html">BaseHTTPServer</a></tt>:</b>
    <dd>Base class implementation for Web server
                             and request handler.
  </dl>
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="18.19 basehttpserver  "
  href="module-BaseHTTPServer.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="18. internet Protocols and"
  href="internet.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="18.21 cgihttpserver  "
  href="module-CGIHTTPServer.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-BaseHTTPServer.html">18.19 BaseHTTPServer  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="internet.html">18. Internet Protocols and</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-CGIHTTPServer.html">18.21 CGIHTTPServer  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
